import java.util.Arrays;

public class Test_3 {

//    二维数组
    public static void main(String[] args) {
        int [][] arr1 = {{1,2,3},{4,5,6}};
        System.out.println(Arrays.deepToString(arr1));
//        直接打印二维数组

        int [][] arr2 = new int [][]{{1,2,3},{4,5,6}};
        for (int [] tmp:arr2) {
            for (int x:tmp) {
                System.out.print(x + " ");
            }
        }
        System.out.println();

        int [][] arr3 = new int [2][3];

//        System.out.println(arr1[0][2]);

        for (int i = 0; i < arr2.length; i++) {
            for (int j = 0; j < arr2[i].length; j++) {
                System.out.print(arr2[i][j] + " ");
            }
            System.out.println();
        }
    }



//    数组逆序

    public static void reverse(int [] arr){
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            for (int i = 0; i < arr.length - 1; i++) {
                int tmp = arr[i];
                arr[i] = arr[arr.length - 1 - i];
                arr[arr.length - 1 - i] = tmp;
            }
            left++;
            right--;
        }
    }

    public static void main6(String[] args) {
        int [] array = {10,20,30,40,50};
        reverse(array);
        System.out.println(Arrays.toString(array));
    }



//    实现冒泡排序：

    public static void bubbleSort(int [] arr){
//        i 表示趟数
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j] > arr[j + 1]){
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }
            }
        }
    }

    public static void main5(String[] args) {
        int [] array = {5,3,2,9,8,1,7,10,6,4};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }




//    数组元素的填充：
    public static void main4(String[] args) {
        int [] arr1 = {1,2,3,4,5,6,7};
        int [] arr2 = {1,2,3,17,5,6,7};
        int [] arr3 = new int[10];
        Arrays.fill(arr3,3,5,-1);//[3,5)
        System.out.println(Arrays.toString(arr3));
    }

//    数组是否相等的比较
    public static void main3(String[] args) {
        int [] arr1 = {1,2,3,4,5,6,7};
        int [] arr2 = {1,2,3,17,5,6,7};
        boolean flag = Arrays.equals(arr1,arr2);
        System.out.println(flag);
    }


//    给数组排序（默认为升序）：
//    Java 自己的二分查找:

    public static void main2(String[] args) {
        int [] array = {2,6,8,3,10,4,9,5,7,1};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.binarySearch(array, 8));
    }

//    二分查找：

    /**
     *
     * @param arr
     * @param key
     * @return 代表的是找到的数据的下标
     */

    public static int mybinarySearch(int [] arr,int key){
        int left = 0;
        int right = arr.length - 1;
        while(left <= right){
            int mid = (left + right) / 2;
            if(arr[mid] < key){
                left = mid + 1;
            }else if(arr[mid] > key){
                right = mid - 1;
            }else if(arr[mid] == key){
                return mid;
            }
        }
        System.out.println("不存在");
        return -1;
    }

    public static void main1(String[] args) {
        int [] array = {1,2,3,4,5,6,7,8,9,10};
        int k = mybinarySearch(array,12);
        System.out.println(k);
        }



}
